package cn.stylefeng.guns.modular.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;

public class HttpClientUtil {

	public static String uploadImage(String path, String baseString, File file) throws ClientProtocolException, IOException {
         // 1. 创建上传需要的元素类型
         // 1.1 装载本地上传图片的文件
         FileBody imageFileBody = new FileBody(file);
         StringBody name = new StringBody("admin", ContentType.TEXT_PLAIN);
         // 2. 将所有需要上传元素打包成HttpEntity对象
         HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("name", name).addPart("file", imageFileBody).build();
         // . 创建HttpPost对象，用于包含信息发送post消息
         HttpPost httpPost = new HttpPost(path);
         httpPost.setEntity(reqEntity);
         // . 创建HttpClient对象，传入httpPost执行发送网络请求的动作
         CloseableHttpClient httpClient = HttpClients.createDefault();
         CloseableHttpResponse response = httpClient.execute(httpPost);
         // . 获取返回的实体内容对象并解析内容
         HttpEntity resultEntity = response.getEntity();
         String responseMessage = "";
         try {
             if (resultEntity != null) {
                 InputStream is = resultEntity.getContent();
                 BufferedReader br = new BufferedReader(new InputStreamReader(is));
                 StringBuffer sb = new StringBuffer();
                 String line = "";
                 while ((line = br.readLine()) != null) {
                     sb.append(line);
                 }
                 responseMessage = sb.toString();
             }
             EntityUtils.consume(resultEntity);
         } finally {
             if (null != response) {
                 response.close();
             }
         }
         return responseMessage;
     }
	
	public static void sendNotice(String pushId,String type,String title){
		try {
			List<String> list = new ArrayList<String>();
			list.add(pushId);
			HttpClient client = HttpClients.createDefault();
			// 要调用的接口方法
			String url = "https://www.idjglobalapp.com/app-servce/v1/api/push/send-notice";
			HttpPost post = new HttpPost(url);
			Map<String, String> map = new HashMap<String, String>();
			map.put("type", type);
			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("pushId", list);
			maps.put("title", title);
			maps.put("content", JSONUtils.toJSONString(map));
			StringEntity s = new StringEntity(JSONUtils.toJSONString(maps)); 
			s.setContentType("application/json");
			post.setEntity(s);
			post.addHeader("content-type", "application/json;charset=UTF-8");
			HttpResponse res = client.execute(post);
			if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				// 返回json格式：
				JSONObject object = JSONObject.parseObject(EntityUtils.toString(res.getEntity()));
				System.out.println(object.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
